/**
 * 
 */
package com.ruyipu.forum.base;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ruyipu.forum.R;

/**
 * @author donald
 * 
 */
public class Header {

	private Activity activity;
	private String title;
	private boolean showBack = true;
	private boolean showRefresh = true;

	private LinearLayout llContainer;
	private TextView tvTitle;
	private Button bBack;
	// private LinearLayout llLoad;
	private Button bRefresh;
	private ProgressBar pbLoad;

	private Animation aHide;
	private Animation aShow;

	/**
	 * 根据资源Id获取标题，并且显示返回按钮和刷新按钮
	 * 
	 * @param activity
	 * @param titleId
	 */
	public Header(Activity activity, int titleId) {
		this(activity, titleId, true, true);
	}

	/**
	 * 创建Header，显示返回按钮和刷新按钮
	 * 
	 * @param activity
	 * @param title
	 */
	public Header(Activity activity, String title) {
		this(activity, title, true, true);
	}

	public Header(Activity activity, int titleId, boolean showBack,
			boolean showRefresh) {
		this(activity, activity.getString(titleId), showBack, showRefresh);
	}

	public Header(Activity activity, String title, boolean showBack,
			boolean showRefresh) {
		this.activity = activity;
		this.title = title;
		this.showBack = showBack;
		this.showRefresh = showRefresh;
		
		makeWidget();
		aHide = AnimationUtils.loadAnimation(activity, R.anim.header_hide);
		aShow = AnimationUtils.loadAnimation(activity, R.anim.header_show);
	}

	/**
	 * 初始化控件
	 */
	private void makeWidget() {

		llContainer = (LinearLayout) activity.findViewById(R.id.llHeader);
		tvTitle = (TextView) activity.findViewById(R.id.tvHeaderTitle);
		// llLoad = (LinearLayout) activity.findViewById(R.id.llHeaderLoad);
		bBack = (Button) activity.findViewById(R.id.bHeaderBack);
		bRefresh = (Button) activity.findViewById(R.id.bHeaderRefresh);
		pbLoad = (ProgressBar) activity.findViewById(R.id.pbHeaderLoad);
		
		setTitle(title);
		initBack();
		initRefresh();
	}

	private void initBack() {
		if(!showBack) {
			bBack.setVisibility(View.INVISIBLE);
		}
	}

	private void initRefresh() {
		if(!showRefresh) {
			bRefresh.setVisibility(View.INVISIBLE);
		}
	}

	/**
	 * 设定标题
	 * 
	 * @param title
	 */
	public void setTitle(String title) {
		if (title.length() > 20) {
			title = title.substring(0, 20) + "...";
		}
		this.title = title;
		tvTitle.setText(title);
	}

	/**
	 * 设定返回按钮的点击事件
	 * 
	 * @param clickListener
	 */
	public void setBackListener(OnClickListener clickListener) {
		bBack.setOnClickListener(clickListener);
	}

	/**
	 * 设定刷新按钮的点击事件
	 * 
	 * @param clickListener
	 */
	public void setRefreshListener(OnClickListener clickListener) {
		bRefresh.setOnClickListener(clickListener);
	}

	/**
	 * 显示Header
	 */
	public void show() {
		llContainer.setVisibility(View.VISIBLE);
		llContainer.startAnimation(aShow);
	}

	/**
	 * 隐藏Header
	 */
	public void hide() {
		llContainer.setVisibility(View.GONE);
		llContainer.startAnimation(aHide);
	}

	/**
	 * 数据加载的时候
	 */
	public void dataLoading() {
		pbLoad.setVisibility(View.VISIBLE);
		bRefresh.setVisibility(View.GONE);
	}

	/**
	 * 数据加载完毕
	 */
	public void dataLoaded() {
		pbLoad.setVisibility(View.GONE);
		if (showRefresh) {
			bRefresh.setVisibility(View.VISIBLE);
		}
	}
}
